{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的定义与调用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    print('execute fn')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute fn\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute fn\n"
     ]
    }
   ],
   "source": [
    "a = fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def add(x, y):\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = 1\n",
    "y = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def swap(x, y):\n",
    "    x, y = y, x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "swap(x,  y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lst = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def append(item):\n",
    "    lst.append(item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "append(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 位置参数和关键字参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "位置参数和关键字参数是在调用的时候决定的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def add(x, y):\n",
    "    print(\"x = {0}\".format(x))\n",
    "    print(\"y = {0}\".format(y))\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 位置参数  通过参数传递的位置来决定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 关键字参数 通过参数名称来决定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(x=1, y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(y=2, x=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 混合使用 关键字参数必须在位置参数之后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(1, y=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-keyword arg after keyword arg (<ipython-input-27-ad039a2c8a00>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-27-ad039a2c8a00>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    add(x=1, 2)\u001b[0m\n\u001b[1;37m            ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-keyword arg after keyword arg\n"
     ]
    }
   ],
   "source": [
    "add(x=1, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def inc(x, i=1):\n",
    "    return x + i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inc(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inc(5, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当默认参赛和关键字参数一起使用的时候，世界都是美好的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (<ipython-input-32-53c6c800feb0>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-32-53c6c800feb0>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def inc(i=1, x):\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "def inc(i=1, x):\n",
    "    return x + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可变参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sum(lst):\n",
    "    ret = 0\n",
    "    for x in lst:\n",
    "        ret += x\n",
    "    return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 可变位置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sum(*args):\n",
    "    ret = 0\n",
    "    print(args)\n",
    "    for x in args:\n",
    "        ret += x\n",
    "    return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(1, 2, 3, 4, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 4, 3, 2, 1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(5, 4, 3, 2, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 可变关键字参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_info(**kwargs):\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a => 1\n",
      "b => 2\n"
     ]
    }
   ],
   "source": [
    "print_info(a=1, b=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "print_info() takes 0 positional arguments but 3 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-45-8434f7cfc5af>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: print_info() takes 0 positional arguments but 3 were given"
     ]
    }
   ],
   "source": [
    "print_info(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sum() got an unexpected keyword argument 'a'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-88d12a0f40cb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: sum() got an unexpected keyword argument 'a'"
     ]
    }
   ],
   "source": [
    "sum(a=1, b=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可变参数函数在定义的时候，就决定了参数是位置参数还是关键字参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_info(*args, **kwargs):\n",
    "    for x in args:\n",
    "        print(x)\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "a => 4\n",
      "b => 5\n"
     ]
    }
   ],
   "source": [
    "print_info(1, 2, 3, a=4, b=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_info(x, y, *args, **kwargs):\n",
    "    print('x = {0}'.format(x))\n",
    "    print('y = {0}'.format(y))\n",
    "    for x in args:\n",
    "        print(x)\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n",
      "4\n",
      "5\n",
      "6\n",
      "a => 7\n",
      "b => 8\n"
     ]
    }
   ],
   "source": [
    "print_info(1, 2, 4, 5, 6, a=7, b=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_info(*args, x, y, **kwargs):\n",
    "    print('x = {0}'.format(x))\n",
    "    print('y = {0}'.format(y))\n",
    "    for x in args:\n",
    "        print(x)\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "print_info() missing 2 required keyword-only arguments: 'x' and 'y'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-54-ae296ebebcdd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint_info\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: print_info() missing 2 required keyword-only arguments: 'x' and 'y'"
     ]
    }
   ],
   "source": [
    "print_info(1, 2, 4, 5, 6, a=7, b=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 5\n",
      "y = 6\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "a => 7\n",
      "b => 8\n"
     ]
    }
   ],
   "source": [
    "print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-56-893faa72b9f1>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-56-893faa72b9f1>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def print_info(*args, **kwargs, x, y):\u001b[0m\n\u001b[1;37m                                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def print_info(*args,  **kwargs,  x, y):\n",
    "    print('x = {0}'.format(x))\n",
    "    print('y = {0}'.format(y))\n",
    "    for x in args:\n",
    "        print(x)\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def print_info( x, y=0, *args, **kwargs):\n",
    "    print('x = {0}'.format(x))\n",
    "    print('y = {0}'.format(y))\n",
    "    for x in args:\n",
    "        print(x)\n",
    "    for k, v in kwargs.items():\n",
    "        print('{0} => {1}'.format(k, v))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 2\n",
      "4\n",
      "5\n",
      "6\n",
      "a => 7\n",
      "b => 8\n"
     ]
    }
   ],
   "source": [
    "print_info(1, 2, 4, 5, 6, a=7, b=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = 1\n",
      "y = 0\n",
      "a => 7\n",
      "b => 8\n"
     ]
    }
   ],
   "source": [
    "print_info(1,  a=7, b=8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 非默认非可变参数， 可变位置参数，可变关键字参数\n",
    "* 默认参数不要和可变参数放到一起"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 参数解包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def add(x, y):\n",
    "    print('x is {0}'.format(x))\n",
    "    print('y is {0}'.format(y))\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "lst = [1, 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 1\n",
      "y is 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(lst[0], lst[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 1\n",
      "y is 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(*lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "d = {'x': 1, 'y': 2}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x is 1\n",
      "y is 2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add(**d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "add() takes 2 positional arguments but 4 were given",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-66-65394eaddff6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: add() takes 2 positional arguments but 4 were given"
     ]
    }
   ],
   "source": [
    "add(*[1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "add() got an unexpected keyword argument 'z'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-67-fa27016a66bc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'y'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'z'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: add() got an unexpected keyword argument 'z'"
     ]
    }
   ],
   "source": [
    "add(**{'x': 1, 'y':2, 'z':3})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "add() missing 1 required positional argument: 'y'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-68-9feb3d25ee96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: add() missing 1 required positional argument: 'y'"
     ]
    }
   ],
   "source": [
    "add(**{'x':1})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 默认参数的坑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn(lst=[]):\n",
    "    lst.append(1)\n",
    "    print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn(lst = None):\n",
    "    if lst is None:\n",
    "        lst = []\n",
    "    lst.append(1)\n",
    "    print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数返回值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NoneType"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(fn())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    return;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NoneType"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(fn())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    return 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    return 3, 4, 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 5)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    for x in range(10):\n",
    "        print(x)\n",
    "        if x > 5:\n",
    "            return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第归函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "x!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1 * 2 * 3 * 4 ...* x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "x * (x-1)!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn(x):\n",
    "    if x == 1:\n",
    "        return x\n",
    "    return fn(x - 1) * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 * 2 * 3 * 4 * 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn(900)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "maximum recursion depth exceeded",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-91-b0298670baef>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-90-9ed819b4f178>\u001b[0m in \u001b[0;36mfn\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "... last 1 frames repeated, from the frame below ...\n",
      "\u001b[1;32m<ipython-input-90-9ed819b4f178>\u001b[0m in \u001b[0;36mfn\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m: maximum recursion depth exceeded"
     ]
    }
   ],
   "source": [
    "fn(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn(x):\n",
    "    ret = 1\n",
    "    for i in range(1, x +1):\n",
    "        ret *= i\n",
    "    return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 作用域与全局变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'xx' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-101-7051982b9152>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mxx\u001b[0m \u001b[1;33m=\u001b[0m  \u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mxx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'xx' is not defined"
     ]
    }
   ],
   "source": [
    "def fn():\n",
    "    xx =  6\n",
    "\n",
    "print(xx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "xx = 6\n",
    "def fn():\n",
    "    print(xx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def fn():\n",
    "    xx = 3\n",
    "    print(xx)\n",
    "fn()\n",
    "print(xx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8978880\n",
      "8978784\n",
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "xx= 6\n",
    "print(id(xx))\n",
    "def fn():\n",
    "    xx = 3\n",
    "    print(id(xx))\n",
    "    print(xx)\n",
    "fn()\n",
    "print(xx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "xx = 6\n",
    "def fn():\n",
    "    global xx\n",
    "    xx = 3\n",
    "    print(xx)\n",
    "fn()\n",
    "print(xx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_i80': 'def fn():\\n    pass', '_24': 3, '_i71': 'fn()', '_i2': 'fn()', '_i31': 'inc(5, 3)', 'inc': <function inc at 0x7f1a0147a8c8>, '_i90': 'def fn(x):\\n    return fn(x - 1) * x', '_i15': 'def append(item):\\n    lst.append(item)\\n    ', '__spec__': None, '_i99': 'fn(5)', '_i93': 'fn(5)', '_65': 3, '_i81': 'type(fn())', '_i33': 'def sum(lst):\\n    ret = 0\\n    for x in lst:\\n        ret += x\\n    return ret', '_i7': 'def add(x, y):\\n    return x + y', '_i17': 'lst', '_i88': 'def fn():\\n    for x in range(10):\\n        print(x)\\n        if x > 5:\\n            return', '_i104': 'def fn():\\n    xx += 3\\n    print(xx)\\nfn()\\nprint(xx)', '_i109': 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i98': 'def fn(x):\\n    ret = 1\\n    for i in range(1, x +1):\\n        ret *= i\\n    return ret', '_i12': 'x', '_i75': 'fn()', 'x': 1, '_i10': 'def swap(x, y):\\n    x, y = y, x', 'fn': <function fn at 0x7f1a00c20730>, '_i28': 'def inc(x, i=1):\\n    return x + i', '_i82': 'def fn():\\n    return;', '_i77': 'fn()', '_i112': 'xx = 6\\ndef fn():\\n    x = 5\\n    print(globals())\\n    print(locals())\\n\\nfn()', '_i105': 'def fn():\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', 'lst': [1, 2], '_i106': 'def fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'Out': {65: 3, 81: <class 'NoneType'>, 8: 3, 87: (3, 4, 5), 12: 1, 13: 2, 17: [1, 2, 3, 4], 19: 3, 20: 3, 21: 3, 23: 3, 24: 3, 25: 3, 26: 3, 29: 6, 94: 120, 31: 8, 97: 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 34: 15, 99: 120, 36: 15, 38: 15, 39: 15, 93: 120, 83: <class 'NoneType'>, 85: 4, 95: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, 62: 3, 63: 3}, '_i87': 'fn()', '_13': 2, '_i92': 'def fn(x):\\n    if x == 1:\\n        return x\\n    return fn(x - 1) * x', '_i84': 'def fn():\\n    return 4', '_i100': 'def fn():\\n    x =  6\\n\\nprint(x)', 'In': ['', 'def fn():\\n    pass', 'fn()', \"def fn():\\n    print('execute fn')\", 'fn()', 'a = fn()', 'a', 'def add(x, y):\\n    return x + y', 'add(1, 2)', 'x = 1\\ny = 2', 'def swap(x, y):\\n    x, y = y, x', 'swap(x,  y)', 'x', 'y', 'lst = [1, 2, 3]', 'def append(item):\\n    lst.append(item)\\n    ', 'append(4)', 'lst', 'def add(x, y):\\n    return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'def add(x, y):\\n    print(\"x = {0}\".format(x))\\n    print(\"y = {0}\".format(y))\\n    return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'add(1, y=2)', 'add(x=1, 2)', 'def inc(x, i=1):\\n    return x + i', 'inc(5)', 'def inc(5, 3)', 'inc(5, 3)', 'def inc(i=1, x):\\n    return x + 1', 'def sum(lst):\\n    ret = 0\\n    for x in lst:\\n        ret += x\\n    return ret', 'sum([1, 2, 3, 4, 5])', 'def sum(*args):\\n    ret = 0\\n    for x in args:\\n        ret += x\\n    return ret', 'sum(1, 2, 3, 4, 5)', 'def sum(*args):\\n    ret = 0\\n    print(args)\\n    for x in args:\\n        ret += x\\n    return ret', 'sum(1, 2, 3, 4, 5)', 'sum(5, 4, 3, 2, 1)', \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, w))\", 'print_info(a=1, b=2)', \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(a=1, b=2)', '* 可变关键字参数', 'print_info(1, 2, 3)', 'sum(a=1, b=2)', \"def print_info(*args, **kwargs):\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 3, a=4, b=5)', \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {1}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(*args, x, y, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', \"def print_info(*args, **kwargs, x, y):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", \"def print_info( x, y=0, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,  a=7, b=8)', \"def add(x, y):\\n    print('x is {0}'.format(x))\\n    print('y is {0}'.format(y))\\n    return x + y\", 'lst = [1, 2]', 'add(lst[0], lst[1])', 'add(*lst)', \"d = {'x': 1, 'y': 2}\", 'add(**d)', 'add(*[1, 2, 3, 4])', \"add(**{'x': 1, 'y':2, 'z':3})\", \"add(**{'x':1})\", 'def fn(lst=[]):\\n    lst.append(1)\\n    print(lst)', 'fn()', 'fn()', 'fn()', 'fn()', 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(a)\\n    print(lst)', 'fn()', 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(1)\\n    print(lst)', 'fn()', 'fn()', 'fn()', 'def fn():\\n    pass', 'type(fn())', 'def fn():\\n    return;', 'type(fn())', 'def fn():\\n    return 4', 'fn()', 'def fn():\\n    return 3, 4, 5', 'fn()', 'def fn():\\n    for x in range(10):\\n        print(x)\\n        if x > 5:\\n            return', 'fn()', 'def fn(x):\\n    return fn(x - 1) * x', 'fn(5)', 'def fn(x):\\n    if x == 1:\\n        return x\\n    return fn(x - 1) * x', 'fn(5)', '1 * 2 * 3 * 4 * 5', 'fn(100)', 'fn(1000)', 'fn(900)', 'def fn(x):\\n    ret = 1\\n    for i in range(1, x +1):\\n        ret *= i\\n    return ret', 'fn(5)', 'def fn():\\n    x =  6\\n\\nprint(x)', 'def fn():\\n    xx =  6\\n\\nprint(xx)', 'xx = 6\\ndef fn():\\n    print(xx)', 'fn()', 'def fn():\\n    xx += 3\\n    print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    #xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\nprint(id(xx))\\ndef fn():\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n    global xx\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n    x = 5\\n    print(globals())\\n    print(locals())\\n\\nfn()'], '__doc__': 'Automatically created module for IPython interactive environment', '_i57': \"def print_info( x, y=0, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_19': 3, '_i69': 'def fn(lst=[]):\\n    lst.append(1)\\n    print(lst)', '_i21': 'add(y=2, x=1)', 'add': <function add at 0x7f1a00c491e0>, '_97': 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, '_31': 8, '_63': 3, '_i101': 'def fn():\\n    xx =  6\\n\\nprint(xx)', '_i46': 'sum(a=1, b=2)', '_i37': 'def sum(*args):\\n    ret = 0\\n    print(args)\\n    for x in args:\\n        ret += x\\n    return ret', 'sum': <function sum at 0x7f1a00c20ae8>, '_oh': {65: 3, 81: <class 'NoneType'>, 8: 3, 87: (3, 4, 5), 12: 1, 13: 2, 17: [1, 2, 3, 4], 19: 3, 20: 3, 21: 3, 23: 3, 24: 3, 25: 3, 26: 3, 29: 6, 94: 120, 31: 8, 97: 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 34: 15, 99: 120, 36: 15, 38: 15, 39: 15, 93: 120, 83: <class 'NoneType'>, 85: 4, 95: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, 62: 3, 63: 3}, '_i22': 'def add(x, y):\\n    print(\"x = {0}\".format(x))\\n    print(\"y = {0}\".format(y))\\n    return x + y', '_i35': 'def sum(*args):\\n    ret = 0\\n    for x in args:\\n        ret += x\\n    return ret', '_i56': \"def print_info(*args, **kwargs, x, y):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_i30': 'def inc(5, 3)', '__loader__': None, '_i43': 'print_info(a=1, b=2)', '_i96': 'fn(1000)', '_29': 6, '_i38': 'sum(1, 2, 3, 4, 5)', '_ii': 'xx= 6\\nprint(id(xx))\\ndef fn():\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i44': '* 可变关键字参数', 'y': 2, '_i86': 'def fn():\\n    return 3, 4, 5', '_i34': 'sum([1, 2, 3, 4, 5])', '_12': 1, 'xx': 6, '_i25': 'add(y=2, x=1)', '_i29': 'inc(5)', 'append': <function append at 0x7f1a0147a840>, '_i26': 'add(1, y=2)', '_i16': 'append(4)', '_i62': 'add(lst[0], lst[1])', '_i95': 'fn(100)', '_i32': 'def inc(i=1, x):\\n    return x + 1', 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f1a01d35518>, '_i5': 'a = fn()', 'swap': <function swap at 0x7f1a0147a158>, '_i74': 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(a)\\n    print(lst)', '_i47': \"def print_info(*args, **kwargs):\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_i108': 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    #xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i78': 'fn()', '_i68': \"add(**{'x':1})\", '_i27': 'add(x=1, 2)', '_62': 3, '_i79': 'fn()', '_i110': 'xx= 6\\nprint(id(xx))\\ndef fn():\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i4': 'fn()', '_95': 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, '_34': 15, '_i3': \"def fn():\\n    print('execute fn')\", '_26': 3, '_8': 3, '_i11': 'swap(x,  y)', '_i45': 'print_info(1, 2, 3)', '_i6': 'a', '_i36': 'sum(1, 2, 3, 4, 5)', '_i102': 'xx = 6\\ndef fn():\\n    print(xx)', '_i70': 'fn()', '_ih': ['', 'def fn():\\n    pass', 'fn()', \"def fn():\\n    print('execute fn')\", 'fn()', 'a = fn()', 'a', 'def add(x, y):\\n    return x + y', 'add(1, 2)', 'x = 1\\ny = 2', 'def swap(x, y):\\n    x, y = y, x', 'swap(x,  y)', 'x', 'y', 'lst = [1, 2, 3]', 'def append(item):\\n    lst.append(item)\\n    ', 'append(4)', 'lst', 'def add(x, y):\\n    return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'def add(x, y):\\n    print(\"x = {0}\".format(x))\\n    print(\"y = {0}\".format(y))\\n    return x + y', 'add(1, 2)', 'add(x=1, y=2)', 'add(y=2, x=1)', 'add(1, y=2)', 'add(x=1, 2)', 'def inc(x, i=1):\\n    return x + i', 'inc(5)', 'def inc(5, 3)', 'inc(5, 3)', 'def inc(i=1, x):\\n    return x + 1', 'def sum(lst):\\n    ret = 0\\n    for x in lst:\\n        ret += x\\n    return ret', 'sum([1, 2, 3, 4, 5])', 'def sum(*args):\\n    ret = 0\\n    for x in args:\\n        ret += x\\n    return ret', 'sum(1, 2, 3, 4, 5)', 'def sum(*args):\\n    ret = 0\\n    print(args)\\n    for x in args:\\n        ret += x\\n    return ret', 'sum(1, 2, 3, 4, 5)', 'sum(5, 4, 3, 2, 1)', \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, w))\", 'print_info(a=1, b=2)', \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(a=1, b=2)', '* 可变关键字参数', 'print_info(1, 2, 3)', 'sum(a=1, b=2)', \"def print_info(*args, **kwargs):\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 3, a=4, b=5)', \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {1}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', \"def print_info(*args, x, y, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', \"def print_info(*args, **kwargs, x, y):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", \"def print_info( x, y=0, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'print_info(1,  a=7, b=8)', \"def add(x, y):\\n    print('x is {0}'.format(x))\\n    print('y is {0}'.format(y))\\n    return x + y\", 'lst = [1, 2]', 'add(lst[0], lst[1])', 'add(*lst)', \"d = {'x': 1, 'y': 2}\", 'add(**d)', 'add(*[1, 2, 3, 4])', \"add(**{'x': 1, 'y':2, 'z':3})\", \"add(**{'x':1})\", 'def fn(lst=[]):\\n    lst.append(1)\\n    print(lst)', 'fn()', 'fn()', 'fn()', 'fn()', 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(a)\\n    print(lst)', 'fn()', 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(1)\\n    print(lst)', 'fn()', 'fn()', 'fn()', 'def fn():\\n    pass', 'type(fn())', 'def fn():\\n    return;', 'type(fn())', 'def fn():\\n    return 4', 'fn()', 'def fn():\\n    return 3, 4, 5', 'fn()', 'def fn():\\n    for x in range(10):\\n        print(x)\\n        if x > 5:\\n            return', 'fn()', 'def fn(x):\\n    return fn(x - 1) * x', 'fn(5)', 'def fn(x):\\n    if x == 1:\\n        return x\\n    return fn(x - 1) * x', 'fn(5)', '1 * 2 * 3 * 4 * 5', 'fn(100)', 'fn(1000)', 'fn(900)', 'def fn(x):\\n    ret = 1\\n    for i in range(1, x +1):\\n        ret *= i\\n    return ret', 'fn(5)', 'def fn():\\n    x =  6\\n\\nprint(x)', 'def fn():\\n    xx =  6\\n\\nprint(xx)', 'xx = 6\\ndef fn():\\n    print(xx)', 'fn()', 'def fn():\\n    xx += 3\\n    print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', 'def fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    #xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx= 6\\nprint(id(xx))\\ndef fn():\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n    global xx\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', 'xx = 6\\ndef fn():\\n    x = 5\\n    print(globals())\\n    print(locals())\\n\\nfn()'], '_i8': 'add(1, 2)', '_i48': 'print_info(1, 2, 3, a=4, b=5)', '_i55': 'print_info(1,2 , 3, 4, x=5, y=6, a=7, b=8)', '_i59': 'print_info(1,  a=7, b=8)', '_20': 3, '_i85': 'fn()', '_i1': 'def fn():\\n    pass', '_i67': \"add(**{'x': 1, 'y':2, 'z':3})\", '___': 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000, '_85': 4, '_25': 3, '_21': 3, '_i97': 'fn(900)', '_i64': \"d = {'x': 1, 'y': 2}\", '_i': 'xx = 6\\ndef fn():\\n    global xx\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', '_i103': 'fn()', '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_39': 15, '__name__': '__main__', '_i61': 'lst = [1, 2]', '_17': [1, 2, 3, 4], '_i50': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_93': 120, '_': 120, '_i19': 'add(1, 2)', '_i39': 'sum(5, 4, 3, 2, 1)', '_36': 15, '_i52': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_i24': 'add(x=1, y=2)', '_iii': 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i66': 'add(*[1, 2, 3, 4])', '_i89': 'fn()', '_99': 120, '_87': (3, 4, 5), '__package__': None, '_i54': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', '_i42': \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_94': 120, '_i73': 'fn()', '_i14': 'lst = [1, 2, 3]', '_i76': 'def fn(lst = None):\\n    if lst is None:\\n        lst = []\\n    lst.append(1)\\n    print(lst)', '_i107': 'xx= 6\\ndef fn():\\n    print(id(xx))\\n    xx = 3\\n    print(id(xx))\\n    print(xx)\\nfn()\\nprint(xx)', '_i91': 'fn(5)', '_i13': 'y', '_i41': 'print_info(a=1, b=2)', '_i58': 'print_info(1, 2, 4, 5, 6, a=7, b=8)', 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7f1a04e06b38>>, '_i65': 'add(**d)', '_i40': \"def print_info(**kwargs):\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, w))\", '_81': <class 'NoneType'>, '_i49': \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {1}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_23': 3, 'a': None, '_i72': 'fn()', '_i20': 'add(x=1, y=2)', '_i83': 'type(fn())', '_sh': <module 'IPython.core.shadowns' from '/home/comyn/.pyenv/versions/3.4.2/envs/magedu_004/lib/python3.4/site-packages/IPython/core/shadowns.py'>, '__': 67526802209645841583879061361800814224269427869589384312198268703685091643180416969132446952698303794226010370578672908593198347699886928591906501031587651846976759681112609524787093848004428636186893395272784450630354080243217646658024696659065951793757223520229235577548653833681102170973893746054649126415909143150172860721156685810655759230011450132992176454983227538696340112610447029002337004887877266387704586077293585433151612518800147764461182680822867092786694982831838641800997499819339206579415325649748486265233918911087114592440896594062675914294925816719862178374679272092637524786939036290035924271782253738059886933923447877769583003016705363339031413069155837518524761078342052635475632113169618774549275701480106933362990003732589370593557325299434734459295866728988740794174654391479926000848846686708729736713207285203712732201272410830836913052635365082888725171636081587151603468291106754640398232146673627370895934090777828827549554232436190464827998683927179246029919443251026464452337939599198528297828591122689960620361238248313158071643395848405047261412680039877733761849874447323867911712630023171745968278465780558568067035013885275080292137360491875164947724464221693533755035300065350065137490832039523382963747026185653050331832380991844842560750923543775188582096487476950254418365198999674684417286265442786651594404781622946901879166382930714196908227460133027605817864877377712193142137625430353718448269390732615776645283198828602917680224041088993892610506802195917247838900106910698057030379190571057605849323113308634452008179881165616449767648354161225066967961297609698742737923389391615207441152319392845687673311899247085327703421862972871644495409572259985563215471482083325653231777113271326579970310755604973969708949477374254974480294652427022436705380184064008853457214518515270985563195412993145274057688634448812449445800617631162768243125606424844709372022149908463572254912654907763445758543980999149122998104378965626781898655221443263601405152073199706585080288735040205417371277253096243200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, '_i23': 'add(1, 2)', 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f1a01d35518>, '_dh': ['/home/comyn/magedu/python/2016/004'], '_i51': \"def print_info(x, y, *args, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", '_i94': '1 * 2 * 3 * 4 * 5', '_38': 15, 'print_info': <function print_info at 0x7f1a00c496a8>, '_i18': 'def add(x, y):\\n    return x + y', '_i111': 'xx = 6\\ndef fn():\\n    global xx\\n    xx = 3\\n    print(xx)\\nfn()\\nprint(xx)', '_i60': \"def add(x, y):\\n    print('x is {0}'.format(x))\\n    print('y is {0}'.format(y))\\n    return x + y\", '_83': <class 'NoneType'>, '_i53': \"def print_info(*args, x, y, **kwargs):\\n    print('x = {0}'.format(x))\\n    print('y = {0}'.format(y))\\n    for x in args:\\n        print(x)\\n    for k, v in kwargs.items():\\n        print('{0} => {1}'.format(k, v))\", 'd': {'y': 2, 'x': 1}, '_i63': 'add(*lst)', '_i9': 'x = 1\\ny = 2'}\n",
      "{'x': 5}\n"
     ]
    }
   ],
   "source": [
    "xx = 6\n",
    "def fn():\n",
    "    x = 5\n",
    "    print(globals())\n",
    "    print(locals())\n",
    "\n",
    "fn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn():\n",
    "    x = 5\n",
    "    print(locals())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'x': 5}\n"
     ]
    }
   ],
   "source": [
    "fn()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数的调用过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def add(x, y):\n",
    "    return x + y\n",
    "\n",
    "def inc(x):\n",
    "    return x+1\n",
    "\n",
    "def main():\n",
    "    x = 1\n",
    "    y = 2\n",
    "    ret = add(x, y)\n",
    "    ret = inc(ret)\n",
    "    print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def add(x, y):\n",
    "    return x + y\n",
    "\n",
    "def inc(x):\n",
    "    return add(x, 1)\n",
    "\n",
    "def main():\n",
    "    x = 5\n",
    "    inc(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def iterator(x):\n",
    "    return list(range(x))\n",
    "\n",
    "iterator(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def iterator(x):\n",
    "    i = 0;\n",
    "    while i < 5:\n",
    "        yield i\n",
    "        i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object iterator at 0x7f1a00b57c18>"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterator(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def main():\n",
    "    for i in iterator(5):\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def iterrator():\n",
    "    lst = [1, 2, 3, 4, 5]\n",
    "    yield from lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for x in iterrator():\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def fn(x):\n",
    "    ret = 1\n",
    "    for i in range(1, x+1):\n",
    "        ret *= i\n",
    "        yield ret\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "6\n",
      "24\n",
      "120\n"
     ]
    }
   ],
   "source": [
    "for x in fn(5):\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object fn at 0x7f1a00a78120>"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "it = fn(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-134-2cdb14c0d4d6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "next(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
